package cn.dalgen.mybatis.gen.utils;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.dalgen.mybatis.gen.model.db.DataBase;
import cn.dalgen.mybatis.gen.utils.db.DruidUtils;
import cn.dalgen.mybatis.gen.utils.db.HikaricpUtils;
import org.apache.maven.plugin.logging.Log;
import org.apache.maven.plugin.logging.SystemStreamLog;

/**
 * Hikaricp数据库连接池工具类的设计
 *
 * @author Mryang
 */
public class DataSourceUtils {

    /**
     * The constant LOG.
     */
    private static final Log        LOG = new SystemStreamLog();
    private static       Connection connection;

    /**
     * 获取数据源
     *
     * @param dataBase
     * @return
     * @throws SQLException
     * @throws IOException
     */
    public static void initDataSource(DataBase dataBase) {
        HikaricpUtils.getDataSource(dataBase);
    }

    /**
     * 通过数据源获取连接
     *
     * @return
     * @throws SQLException
     */
    public static Connection getConnection() throws SQLException {
        if(connection!=null){
            connection.close();
            connection=null;
        }
        connection= HikaricpUtils.getConnection();
        return connection;
    }

    public static void close() {
        if (connection != null) {
            connection=null;
            HikaricpUtils.close();
        }
    }
}